package com.breeze.components.log.provider.json;

import ch.qos.logback.classic.spi.ILoggingEvent;
import com.breeze.components.core.util.web.RequestContextUtil;
import net.logstash.logback.fieldnames.LogstashFieldNames;

/**
 * @author breeze
 */
public class ClientIpJsonProvider extends BaseJsonProvider {

  @Override
  public void setFieldNames(LogstashFieldNames fieldNames) {
    this.setFieldName("client_ip");
  }

  @Override
  public String getValue(ILoggingEvent event) {
    try {
      Class.forName("org.springframework.web.context.request.RequestContextHolder");
      return RequestContextUtil.getClientIp();
    } catch (Exception ignore) {
    }
    return null;
  }
}
